/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package akheer.safe;

import java.io.IOException;
import java.util.Observable;
import java.util.ArrayList;
import java.util.Observer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import akheer.safe.net.RabbitClient;
import akheer.safe.net.RabbitClientMessage;
import akheer.safe.net.RabbitClientMessageType;

/**
 *
 * @author Saad
 */
public class LiveStore implements Observer {

    private static final Logger log = LoggerFactory.getLogger("LS");
    
    private final RabbitClient rc;
    private ArrayList<String> store;
    
    public LiveStore() throws IOException {

        rc = new RabbitClient("LS", "lsIN", "lsIN");
        rc.addObserver(this);
        
        
        store = new ArrayList<String>();
    }
    
    public void start() {
        rc.startListening("lsIN", false);
    }
    
    public void stop() {
        rc.stopListening();
    }
    
    public void dispose() {
        rc.stopListening();
    }
    
    public boolean addToStore(String value) {
        return store.add(value);
    }
    
    public boolean checkValueInStore(String value) {
        return store.contains(value);
    }
    
    @Override
    public void update(Observable o, Object arg) {
        
        try {
            
            RabbitClientMessage rcmIn = (RabbitClientMessage)arg;
            
            DrugQuery drugQuery = new DrugQuery(rcmIn.getMessage());
            
            boolean success = checkValueInStore(drugQuery.getUniqueID());
            
            if ( success ) drugQuery.setAuthenticity(Authenticity.AUTHENTIC);
            else drugQuery.setAuthenticity(Authenticity.NOT_AUTEHNTIC);
            
            log.info("UID:={} {}", drugQuery.getUniqueID(), success);
            
            RabbitClientMessage rcmOut = new RabbitClientMessage();
            rcmOut.setMessage(drugQuery.toString());
            rcmOut.setMessageType(RabbitClientMessageType.QUERY_DRUG_RESULT);
            
            rc.sendMessage("SMSC", "smscIN", rcmOut);
        }
        catch(Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
    
    public static void main(String[] args) {
        
        try {
            
            boolean isDebug = true;
            String propertyFileName = "";
            
            if ( args.length > 0 ) {

                isDebug = false;
                propertyFileName = args[0];
                PropertyLoader.LOAD_RESOURCE_TYPE = PropertyLoader.LoadResourceType.URL;
            }
            
            ApplicationConfig.GetObject(propertyFileName);
            
            LiveStore store = new LiveStore();
            store.addToStore("0123456789");
            store.start();
            
            while( true && !isDebug ) {

                String line = System.console().readLine();
                
                if( line.equalsIgnoreCase("EXIT") ) {
                    break;
                }                
            }
        }        
        catch(Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        
        System.exit(0);
    }
}
